<template>
  <div class="swiper-container">
    <Swiper
      class="swiper"
      :modules="[Navigation]"
      :navigation="navigation"
      @swiper="onSwiper"
      @slide-change="onSlideChange"
      :loop="true"
    >
      <SwiperSlide class="swiper-slide" v-for="i in 6" :key="i">
        <div class="slide-content">
          <div class="slide-number">{{ i }}</div>
          <h3>自定义导航 {{ i }}</h3>
          <p>这是自定义导航的演示内容</p>
        </div>
      </SwiperSlide>
    </Swiper>

    <!-- 顶部导航栏 -->
    <div class="top-navigation">
      <div class="nav-title">图片轮播</div>
      <div class="nav-controls">
        <button class="nav-btn prev-btn" @click="slidePrev" :disabled="isFirstSlide">
          <span>上一页</span>
        </button>
        <div class="slide-counter">
          {{ currentSlide }} / {{ totalSlides }}
        </div>
        <button class="nav-btn next-btn" @click="slideNext" :disabled="isLastSlide">
          <span>下一页</span>
        </button>
      </div>
    </div>

    <!-- 侧边导航按钮 -->
    <div class="side-navigation">
      <button class="side-prev" @click="slidePrev" :disabled="isFirstSlide">
        <i class="arrow-left">←</i>
      </button>
      <button class="side-next" @click="slideNext" :disabled="isLastSlide">
        <i class="arrow-right">→</i>
      </button>
    </div>

    <!-- 底部缩略图导航 -->
    <div class="thumbnail-navigation">
      <div
        class="thumbnail-item"
        v-for="i in totalSlides"
        :key="i"
        :class="{ active: i === currentSlide }"
        @click="goToSlide(i - 1)"
      >
        <div class="thumbnail-number">{{ i }}</div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { Swiper, SwiperSlide } from 'swiper/vue';
import { Navigation } from 'swiper/modules';
import 'swiper/css/navigation';
import 'swiper/css';
import { ref, computed } from 'vue';

// 自定义导航配置
const navigation = {
  nextEl: '.side-next',
  prevEl: '.side-prev',
  disabledClass: 'disabled',
}

// 响应式变量
const swiperInstance = ref<any>(null)
const currentSlide = ref(1)
const totalSlides = ref(6)

// 计算属性
const isFirstSlide = computed(() => currentSlide.value === 1)
const isLastSlide = computed(() => currentSlide.value === totalSlides.value)

// Swiper 实例回调
const onSwiper = (swiper: any) => {
  swiperInstance.value = swiper
}

// 滑动变化回调
const onSlideChange = (swiper: any) => {
  currentSlide.value = swiper.realIndex + 1
}

// 自定义导航方法
const slidePrev = () => {
  swiperInstance.value?.slidePrev()
}

const slideNext = () => {
  swiperInstance.value?.slideNext()
}

const goToSlide = (index: number) => {
  swiperInstance.value?.slideToLoop(index)
}
</script>
<style scoped lang="scss">
.swiper-container {
  width: 100%;
  height: 100%;
  position: relative;

  .swiper {
    width: 100%;
    height: 100%;
  }

  .swiper-slide {
    background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
    width: 100% !important;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    color: #fff;

    .slide-content {
      text-align: center;

      .slide-number {
        width: 60px;
        height: 60px;
        background: rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        font-weight: bold;
        margin: 0 auto 20px;
      }

      h3 {
        margin: 0 0 10px 0;
        font-size: 28px;
      }

      p {
        margin: 0;
        font-size: 16px;
        opacity: 0.9;
      }
    }
  }
}

// 顶部导航栏
.top-navigation {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 10;

  .nav-title {
    color: white;
    font-size: 18px;
    font-weight: bold;
  }

  .nav-controls {
    display: flex;
    align-items: center;
    gap: 15px;

    .nav-btn {
      padding: 8px 16px;
      border: 1px solid rgba(255, 255, 255, 0.3);
      background: rgba(255, 255, 255, 0.1);
      color: white;
      border-radius: 4px;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover:not(:disabled) {
        background: rgba(255, 255, 255, 0.2);
        border-color: rgba(255, 255, 255, 0.5);
      }

      &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
      }
    }

    .slide-counter {
      color: white;
      font-size: 14px;
      min-width: 50px;
      text-align: center;
    }
  }
}

// 侧边导航按钮
.side-navigation {
  .side-prev,
  .side-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 50px;
    background: rgba(0, 0, 0, 0.6);
    border: none;
    color: white;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 10;

    &:hover:not(.disabled) {
      background: rgba(0, 0, 0, 0.8);
    }

    &.disabled {
      opacity: 0.3;
      cursor: not-allowed;
    }
  }

  .side-prev {
    left: 10px;
    border-radius: 0 25px 25px 0;
  }

  .side-next {
    right: 10px;
    border-radius: 25px 0 0 25px;
  }
}

// 底部缩略图导航
.thumbnail-navigation {
  position: absolute;
  bottom: 15px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 10;

  .thumbnail-item {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;

    .thumbnail-number {
      color: white;
      font-size: 14px;
      font-weight: bold;
    }

    &:hover {
      background: rgba(255, 255, 255, 0.4);
      transform: scale(1.1);
    }

    &.active {
      background: rgba(255, 255, 255, 0.8);

      .thumbnail-number {
        color: #333;
      }
    }
  }
}

// 隐藏默认导航按钮
:deep(.swiper-button-prev),
:deep(.swiper-button-next) {
  display: none;
}
</style>
